package com.tutu.controller.backend;

import com.tutu.common.DataTableFactory;
import com.tutu.controller.common.CommonController;
import com.tutu.entity.vo.BaseCodeVo;
import com.tutu.service.CompanyService;
import com.tutu.service.LogInfoService;
import com.tutu.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 公司管理
 */
@Controller
@RequestMapping(value = "backend/company")
public class CompanyController extends CommonController {

    @Autowired
    CompanyService companyService;

    @Autowired
    LogInfoService logInfoService;

    @RequestMapping(value = "/index")
    public String index() {
        return "backend/company/list";
    }

    @RequestMapping(value = "/child/index")
    public String childIndex(Model model, Integer id, Integer pageNum) {
        model.addAttribute("company", companyService.getInfoById(id));
        model.addAttribute("pageNum", pageNum);

        return "backend/company/childList";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response, String keyWord, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, companyService.pageByParams(keyWord, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/child/list")
    public void childList(HttpServletResponse response, Integer companyId, String keyWord, Integer type, Integer draw, Integer start, Integer length) {
        int pageNum = getPageNum(start, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, companyService.pageByParams(companyId, keyWord, type, pageNum, length));

        WebUtil.printJson(response, result);
    }

    @RequestMapping("/findUserListByCompanyId")
    @ResponseBody
    public List<BaseCodeVo> findUserListByCompanyId(Integer companyId) {
        return companyService.findUserListByCompanyId(companyId);
    }

    @RequestMapping("/updateCompanyUser")
    @ResponseBody
    public Map<String, Object> updateCompanyUser(HttpServletRequest request, Integer companyId, Integer selectCompanyId, String ids) {
        return companyService.updateCompanyUser(request, companyId, selectCompanyId, ids);
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> save(HttpServletRequest request, Integer id, String name, String endTime) {
        return companyService.saveInfo(request, id, name, endTime);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> del(HttpServletRequest request, Integer id) {
        return companyService.deleteById(request, id);
    }

    @RequestMapping("/batchDel")
    @ResponseBody
    public Map<String, Object> batchDel(HttpServletRequest request, String ids) {
        return companyService.batchDel(request, ids);
    }

    @RequestMapping("/setContact")
    @ResponseBody
    public Map<String, Object> setContact(HttpServletRequest request, Integer companyId, Integer userId, Integer type) {
        return companyService.setContact(request, companyId, userId, type);
    }

    @RequestMapping("/cleanContact")
    @ResponseBody
    public Map<String, Object> cleanContact(HttpServletRequest request, Integer companyId) {
        return companyService.cleanContact(request, companyId);
    }

    @RequestMapping("/sync")
    @ResponseBody
    public Map<String, Object> sync(HttpServletRequest request) {
        return companyService.syncData(request);
    }

    @RequestMapping("/mergeInfo")
    @ResponseBody
    public Map<String, Object> mergeInfo(HttpServletRequest request, Integer mainCompanyId, Integer otherCompanyId) {
        return companyService.mergeInfo(request, mainCompanyId, otherCompanyId);
    }
}
